package com.znyj.servlet.mainController;

import com.znyj.servlet.entity.TaskEntity;
import com.znyj.servlet.model.TaskQueryModel;
import com.znyj.servlet.service.TaskService;
import com.znyj.servlet.vo.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.ParseException;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * Created by gaoliwei02 on 17/6/25.
 */
@Controller
@RequestMapping(value = "/task")
public class TaskController {
    private static final Logger logger = LoggerFactory.getLogger(TaskController.class);

    @Resource
    private TaskService taskService;

    @RequestMapping(value = "/getlist.inner", method = RequestMethod.POST)
    protected @ResponseBody String getlistInner(@RequestBody String receiveData) throws ParseException, IOException {
        TaskQueryModel taskQueryModel = new TaskQueryModel(receiveData);//string到pojo
        Page<TaskEntity> page = taskService.createTaskPage(taskQueryModel);//从数据库获取实体列表

        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").disableHtmlEscaping().create();
        return gson.toJson(page);
    }


    /*-----------------------------PAGE-----------------------------------------------------*/
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    protected String index(HttpServletRequest request) throws ParseException, IOException {
        return "/page/task_page";
    }
}
